Micron Document
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
| SparkN0de-git | SparkN0de |
--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Commit f107a9ea306d59cac2dc39ffb94fbe53a165d5d2


Parents : 4eed5ba
Author : Mark Qvist <mark@unsigned.io>
Date : 2025-05-11T16:42:50+02:00

Use RNS file responses for file downloads instead of reading entire file into memory

Changes

2 files changed, 34 insertions(+), 24 deletions(-)


Diff

diff --git a/nomadnet/Node.py b/nomadnet/Node.py
index 5c2a3e0..755c002 100644
--- a/nomadnet/Node.py
+++ b/nomadnet/Node.py
@@ -61,16 +61,14 @@ class Node:
self.destination.register_request_handler(
"/page/index.mu",
response_generator = self.serve_default_index,
- allow = RNS.Destination.ALLOW_ALL
- )
+ allow = RNS.Destination.ALLOW_ALL)
for page in self.servedpages:
request_path = "/page"+page.replace(self.app.pagespath, "")
self.destination.register_request_handler(
request_path,
response_generator = self.serve_page,
- allow = RNS.Destination.ALLOW_ALL
- )
+ allow = RNS.Destination.ALLOW_ALL)
def register_files(self):
# TODO: Deregister previously registered files
@@ -83,8 +81,8 @@ class Node:
self.destination.register_request_handler(
request_path,
response_generator = self.serve_file,
- allow = RNS.Destination.ALLOW_ALL
- )
+ allow = RNS.Destination.ALLOW_ALL,
+ auto_compress = 32_000_000)
def scan_pages(self, base_path):
files = [file for file in os.listdir(base_path) if os.path.isfile(os.path.join(base_path, file)) and file[:1] != "."]
@@ -205,10 +203,7 @@ class Node:
file_name = path.replace("/file/", "", 1)
try:
RNS.log("Serving file: "+file_path, RNS.LOG_VERBOSE)
- fh = open(file_path, "rb")
- file_data = fh.read()
- fh.close()
- return [file_name, file_data]
+ return [open(file_path, "rb"), {"name": file_name.encode("utf-8")}]
except Exception as e:
RNS.log("Error occurred while handling request "+RNS.prettyhexrep(request_id)+" for: "+str(path), RNS.LOG_ERROR)

diff --git a/nomadnet/ui/textui/Browser.py b/nomadnet/ui/textui/Browser.py
index 5e412a4..c465dff 100644
--- a/nomadnet/ui/textui/Browser.py
+++ b/nomadnet/ui/textui/Browser.py
@@ -1,8 +1,10 @@
import RNS
import LXMF
+import io
import os
import time
import urwid
+import shutil
import nomadnet
import subprocess
import threading
@@ -1036,23 +1038,36 @@ class Browser:
def file_received(self, request_receipt):
try:
- file_name = request_receipt.response[0]
- file_data = request_receipt.response[1]
- file_destination_name = file_name.split("/")
- file_destination_name = file_destination_name[len(file_destination_name)-1]
- file_destination = self.app.downloads_path+"/"+file_destination_name
+ if type(request_receipt.response) == io.BufferedReader:
+ if request_receipt.metadata != None:
+ file_name = os.path.basename(request_receipt.metadata["name"].decode("utf-8"))
+ file_handle = request_receipt.response
+ file_destination = self.app.downloads_path+"/"+file_name
-
- counter = 0
- while os.path.isfile(file_destination):
- counter += 1
- file_destination = self.app.downloads_path+"/"+file_name+"."+str(counter)
+ counter = 0
+ while os.path.isfile(file_destination):
+ counter += 1
+ file_destination = self.app.downloads_path+"/"+file_name+"."+str(counter)
+
+ shutil.move(file_handle.name, file_destination)
+
+ else:
+ file_name = request_receipt.response[0]
+ file_data = request_receipt.response[1]
+ file_destination_name = file_name.split("/")
+ file_destination_name = file_destination_name[len(file_destination_name)-1]
+ file_destination = self.app.downloads_path+"/"+file_destination_name
+
+ counter = 0
+ while os.path.isfile(file_destination):
+ counter += 1
+ file_destination = self.app.downloads_path+"/"+file_name+"."+str(counter)
- fh = open(file_destination, "wb")
- fh.write(file_data)
- fh.close()
+ fh = open(file_destination, "wb")
+ fh.write(file_data)
+ fh.close()
- self.saved_file_name = file_destination.replace(self.app.downloads_path+"/", "", 1)
+ self.saved_file_name = file_destination.replace(self.app.downloads_path+"/", "", 1)
self.status = Browser.DONE
self.response_progress = 0


──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────